var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
const expressSession = require('express-session');
const chalk = require('chalk')

var indexRouter = require('./routes/index');

var app = express(); 


app.use(expressSession({
  secret:'hello aacau',// cookie签名 这个属性是必须的 具体配置和`cookie-parser`一样
  saveUninitialized:true, // 是否自动初始化 默认为true
  resave:false,// 当用户session无变化的时候依然自动保存
  cookie:{ // cookie的信息具体操作和`cookie-parser`一样
    maxAge:1800000// 30分钟后过期
  },
  rolling:true// 每次请求的时候覆写cookie
}))

app.use(bodyParser.urlencoded({ extended:true }));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'static')));

app.use('/', indexRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  console.log(chalk.red(err));
  res.send('error');
});

module.exports = app;
